using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Edu.Http;
using Edu.Http.Extensions;
using Edu.Http.Proxy;
using Infrastructure.Config.Core.External;
using Infrastructure.ExternalService.Core.PersonOrder;
using Infrastructure.ExternalService.Core.Responses;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Infrastructure.ExternalService.Core.Imp.PersonOrder
{
    /// <summary>
    /// 商品查询服务
    /// </summary>
    public class PersonOrderExternal : BaseHttpClient, IPersonOrderExternal
    {
        private readonly OrderCenterApiOptions _orderingOptions;
        public IAccessTokenProvider TokenProvider { get; set; }

        public PersonOrderExternal(IOptions<OrderCenterApiOptions> options, HttpClient client,
            ILoggerFactory loggerFactory)
            : base(client, loggerFactory, options.Value.PersonOrderOptions.EndPoint)
        {
            _orderingOptions = options.Value ?? throw new HttpProxyException($"{nameof(PersonOrderExternal)}配置获取失败");
            loggerFactory.CreateLogger<PersonOrderExternal>();
        }

        /// <summary>
        /// 设置BaseHttpClient的BaseAddress：该值默认是从构造函数传入的，但重写该方法可以改变该值
        /// </summary>
        /// <returns></returns>
        protected override string SetHttpClientBaseAddress()
        {
            return null;
        }

        public async Task<List<TradeInfo>> GetTradeInfoAsync(GetTradeInfoRequest request)
        {
            var response =
                await PostAsync<WrapResponse<List<TradeInfo>>>(_orderingOptions.PersonOrderOptions.GetTradeInfo,
                    request);
            if (response.Code.Equals(ClientReturnCode.Success) && response.Data != null)
            {
                return response.Data;
            }

            throw new HttpProxyException($"{response.Msg}");
        }
    }
}